package com.wan37.logic.handler.message;

import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.vo.AllRoleVO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.domain.vo.RoleVO;
import com.wan37.logic.global.HandlerManager;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.service.cache.MonsterServiceCache;
import com.wan37.logic.service.cache.RoleServiceCache;
import com.wan37.logic.service.cache.SceneServiceCache;
import com.wan37.logic.service.cache.UserServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.AllRoleLabel;
import com.wan37.protobuf.constant.Common;
import com.wan37.protobuf.constant.CommonBackpack;
import com.wan37.protobuf.constant.PtoNum;
import com.wan37.protobuf.message.client2server.game.Game;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : luoyong
 * @date : 2020-05-29 10:12
 **/
public class CGameAoiHandler extends MsgHandler {

    private static final Logger logger = LoggerFactory.getLogger(CGameAoiHandler.class);
    private RoleServiceCache roleServiceCache;
    private SceneServiceCache sceneServiceCache;
    private RouteUtils routeUtils;
    private UserServiceCache userServiceCache;
    private MonsterServiceCache monsterServiceCache;


    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameAoiHandler(MessageVO messageVO) {
        super(messageVO);
        this.roleServiceCache = SpringContextHolder.getBean("roleServiceCache");
        this.sceneServiceCache = SpringContextHolder.getBean("sceneServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.userServiceCache = SpringContextHolder.getBean("userServiceCache");
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.monsterServiceCache = SpringContextHolder.getBean("monsterServiceCache");
    }


    /**
     * 显示场景下的所有实体
     *
     * @param worker
     * @throws Exception
     */
    @Override
    public void execute(Worker worker) throws Exception {
        Game.CGameAoi cGameAoi = (Game.CGameAoi) messageVO.getMessage();
        String userId = cGameAoi.getSenderId();
        String scene = cGameAoi.getScene();
        if (!isScene(scene)) {
            logger.info("操作有误");
            routeUtils.sendGameAoiResponse(Common.GAME_AOI_ERROR,
                    "操作有误", new ArrayList<>(), messageVO.getNetId(), userId);
            return;
        }
        //获取场景Id
        String sceneId = sceneServiceCache.sceneNameToIdMap.get(scene);
        getAllRole(userId, sceneId);
    }

    public void getAllRole(String userId, String sceneId) throws IllegalAccessException, InstantiationException, InvocationTargetException {

        StringBuilder content = new StringBuilder();
        //预留的一个返回信息内容
        content.append("预留");
        //当前场景下实体的集合
        List<AllRoleVO> allRoleVOList = new ArrayList<>();
        List<String> monsterIdList = new ArrayList<>();
        /**
         * 获取场景的实体返回
         */
        sceneServiceCache.getSceneServiceMap().get(sceneId).getAllRoleVOMap().values().forEach(allRoleVO -> {
            allRoleVOList.add(allRoleVO);
            if (allRoleVO.getLabel() == AllRoleLabel.MONSTER) {
                monsterIdList.add(allRoleVO.getAllRoleId());
            }
        });
        String sceneDOName = sceneServiceCache.getSceneServiceMap().get(sceneId).getSceneDO().getName();
        if (sceneDOName == null || sceneDOName.equals("")) {
            routeUtils.sendGameAoiResponse(Common.GAME_BOSS_SCENE_SUCCESS,
                    content.toString(), allRoleVOList, messageVO.getNetId(), userId);
        } else {
            routeUtils.sendGameAoiResponse(Common.GAME_AOI_SUCCESS,
                    content.toString(), allRoleVOList, messageVO.getNetId(), userId);
        }
        CGameRoleHandler cGameRoleHandler = (CGameRoleHandler) HandlerManager.getMsgHandler
                (PtoNum.CGAMEROLEMSG, messageVO.getUserId(), messageVO.getNetId(), null, messageVO.getCtx());
        cGameRoleHandler.getRole(userId, sceneId);

        /**
         * 这里是获取场景中怪物的技能
         */
        CGameSkillHandler cGameSkillHandler = (CGameSkillHandler) HandlerManager.getMsgHandler
                (PtoNum.CGAMESKILLMSG, userId, messageVO.getNetId(), null, messageVO.getCtx());
        cGameSkillHandler.getSkill(monsterIdList);

        logger.info("显示场景实体数据已返回客户端！！！！请求方userId:" + userId);
    }

    /**
     * 场景Id是否存在
     */
    private boolean isScene(String nextScene) {
        if (nextScene != null) {
            return sceneServiceCache.sceneSet.contains(nextScene);
        } else {
            return false;
        }
    }
}
